package com.fanjia.rabbitmq_producter.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @author fanjia
 * @className OrderService
 * @description TODO
 * @date 2021/4/18 9:35
 */
@Service
public class OrderService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /*
     * @author fanjia
     * @description 模拟用户下单
     * @date  2021/4/18 9:37
     * @param userId
    * @param productId
    * @param num
     * @return void
     **/
    public void makeOrder(String userId,String productId,int num){
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功");
        String exchangeName = "fanout_order_exchange";

        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);


    }



    /*
     * @author fanjia
     * @description 模拟用户下单
     * @date  2021/4/18 9:37
     * @param userId
     * @param productId
     * @param num
     * @return void
     **/
    public void directMakeOrder(String userId,String productId,int num){
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功");
        String exchangeName = "direct_order_exchange";

        String routingKey = "";
        rabbitTemplate.convertAndSend(exchangeName,"sms",orderId);
        rabbitTemplate.convertAndSend(exchangeName,"email",orderId);
    }

    public void topicMakeOrder(String userId,String productId,int num){
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功");
        String exchangeName = "topic_order_exchange";

        String routingKey = "email";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);

    }

    public void ttlTopicMakeOrder(String userId,String productId,int num){
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功");
        String exchangeName = "ttl_direct_order_exchange";

        String routingKey = "ttl";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);

    }




    public void ttlMessageTopicMakeOrder(String userId,String productId,int num){
        String orderId = UUID.randomUUID().toString();
        System.out.println("订单生成成功");
        String exchangeName = "ttl_direct_order_exchange";

        String routingKey = "ttlMessage";
        //给消息设置过期时间
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {

            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("UTF-8");

                return message;
            }
        };
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId,messagePostProcessor);

    }





}
